package com.example.demo.service;

import com.example.demo.dto.TimeDataResponse;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;

@Service
public class TimeDataService {
    public List<TimeDataResponse> generateTimeData(LocalDateTime startTime, LocalDateTime endTime) {
        List<TimeDataResponse> response = new ArrayList<>();
        long hours = ChronoUnit.HOURS.between(startTime, endTime);

        if (hours <= 24) {
            response = generateHourlyData(startTime, endTime);
        } else if (hours <= 168) {
            response = generateDailyData(startTime, endTime);
        } else if (hours <= 720) {
            response = generateWeeklyData(startTime, endTime);
        } else {
            response = generateMonthlyData(startTime, endTime);
        }

        return response;
    }

    private List<TimeDataResponse> generateHourlyData(LocalDateTime start, LocalDateTime end) {
        List<TimeDataResponse> data = new ArrayList<>();
        LocalDateTime current = start;

        while (!current.isAfter(end)) {
            data.add(new TimeDataResponse(
                    current,
                    Math.random() * 100,
                    "小时数据"));
            current = current.plusHours(1);
        }

        return data;
    }

    private List<TimeDataResponse> generateDailyData(LocalDateTime start, LocalDateTime end) {
        List<TimeDataResponse> data = new ArrayList<>();
        LocalDateTime current = start.withHour(0).withMinute(0).withSecond(0);

        while (!current.isAfter(end)) {
            data.add(new TimeDataResponse(
                    current,
                    Math.random() * 1000,
                    "日数据"));
            current = current.plusDays(1);
        }

        return data;
    }

    private List<TimeDataResponse> generateWeeklyData(LocalDateTime start, LocalDateTime end) {
        List<TimeDataResponse> data = new ArrayList<>();
        LocalDateTime current = start.withHour(0).withMinute(0).withSecond(0)
                .with(java.time.temporal.TemporalAdjusters.previousOrSame(java.time.DayOfWeek.MONDAY));

        while (!current.isAfter(end)) {
            data.add(new TimeDataResponse(
                    current,
                    Math.random() * 5000,
                    "周数据"));
            current = current.plusWeeks(1);
        }

        return data;
    }

    private List<TimeDataResponse> generateMonthlyData(LocalDateTime start, LocalDateTime end) {
        List<TimeDataResponse> data = new ArrayList<>();
        LocalDateTime current = start.withDayOfMonth(1).withHour(0).withMinute(0).withSecond(0);

        while (!current.isAfter(end)) {
            data.add(new TimeDataResponse(
                    current,
                    Math.random() * 20000,
                    "月数据"));
            current = current.plusMonths(1);
        }

        return data;
    }
}
